/////////////////////////////////////////////////////////////////////////////
// Name:        DownloadIniDialog.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     08/01/2009 13:54:31
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "DownloadIniDialog.h"
#include "events.h"
#include "common.h"

////@begin XPM images
////@end XPM images


/*!
 * DownloadIniPanel type definition
 */

IMPLEMENT_DYNAMIC_CLASS( DownloadIniPanel, wxPanel )


/*!
 * DownloadIniPanel event table definition
 */

BEGIN_EVENT_TABLE( DownloadIniPanel, wxPanel )

////@begin DownloadIniPanel event table entries
////@end DownloadIniPanel event table entries
	EVT_COMMAND(ID_DOWNLOAD_INI_DIALOG, wxEVT_INI_DOWNLOAD_BEGIN_EVENT, DownloadIniPanel::OnBeginDownload)
	EVT_COMMAND(ID_DOWNLOAD_INI_DIALOG, wxEVT_INI_DOWNLOADING_EVENT, DownloadIniPanel::OnDownloading)
	EVT_COMMAND(ID_DOWNLOAD_INI_DIALOG, wxEVT_INI_DOWNLOAD_COMPLETED_EVENT, DownloadIniPanel::OnDownloadCompleted)
END_EVENT_TABLE()


/*!
 * DownloadIniPanel constructors
 */

DownloadIniPanel::DownloadIniPanel()
{
    Init();
}

DownloadIniPanel::DownloadIniPanel( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}


/*!
 * DownloadIniDialog creator
 */

bool DownloadIniPanel::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin DownloadIniPanel creation
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxPanel::Create( parent, id, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end DownloadIniPanel creation
    
    return true;
}


/*!
 * DownloadIniPanel destructor
 */

DownloadIniPanel::~DownloadIniPanel()
{
////@begin DownloadIniPanel destruction
////@end DownloadIniPanel destruction
    EventCenter::get().unregister(this);
}


/*!
 * Member initialisation
 */

void DownloadIniPanel::Init()
{
////@begin DownloadIniPanel member initialisation
    p_download_info_static = NULL;
    p_download_progress_static = NULL;
    p_download_gauge = NULL;
////@end DownloadIniPanel member initialisation
}


/*!
 * Control creation for DownloadIniDialog
 */

void DownloadIniPanel::CreateControls()
{    
////@begin DownloadIniPanel content construction
    DownloadIniPanel* itemPanel1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    wxStaticText* itemStaticText3 = new wxStaticText( itemPanel1, wxID_STATIC, _("Downloading..."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText3, 0, wxGROW|wxALL, 5);

    p_download_info_static = new wxStaticText( itemPanel1, wxID_STATIC, _("setup.bz2 from "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(p_download_info_static, 0, wxGROW|wxALL, 5);

    p_download_progress_static = new wxStaticText( itemPanel1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(p_download_progress_static, 0, wxGROW|wxALL, 5);

    wxStaticLine* itemStaticLine6 = new wxStaticLine( itemPanel1, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    itemBoxSizer2->Add(itemStaticLine6, 0, wxGROW|wxALL, 5);

    p_download_gauge = new wxGauge( itemPanel1, ID_GAUGE, 100, wxDefaultPosition, wxSize(250, -1), wxGA_HORIZONTAL );
    p_download_gauge->SetValue(0);
    itemBoxSizer2->Add(p_download_gauge, 0, wxGROW|wxALL, 5);

////@end DownloadIniPanel content construction

    EventCenter::get().registerr(wxEVT_INI_DOWNLOAD_BEGIN_EVENT, this);
    EventCenter::get().registerr(wxEVT_INI_DOWNLOADING_EVENT, this);
    EventCenter::get().registerr(wxEVT_INI_DOWNLOAD_COMPLETED_EVENT, this);
    
    _file_size = 0;
    _watch.Pause();
}


/*!
 * Should we show tooltips?
 */

bool DownloadIniPanel::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap DownloadIniPanel::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin DownloadIniPanel bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end DownloadIniPanel bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon DownloadIniPanel::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin DownloadIniPanel icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end DownloadIniPanel icon retrieval
}


void DownloadIniPanel::OnBeginDownload(wxCommandEvent &event) {
	_file_size = event.GetInt();
	
	// get and show file size
	if (_file_size<=0) {
		_file_size_str = _("Unknown size");
		_file_size = 0;
	} else {
		_file_size_str = format_size(size_t(_file_size));
	}
	
	p_download_info_static->SetLabel(
			wxString::Format(_("Downloading setup.bz2 from : \n  Size : %s\n  Speed : %s"),
					_file_size_str.c_str(), wxString().c_str())
	);
	
	_watch.Start();
}

void DownloadIniPanel::OnDownloading(wxCommandEvent &event) {
	// get progress
	int downloaded = event.GetInt();
	int i = 0;
	
	if (_file_size>0) {
		i = (double(downloaded)/double(_file_size))*100;
	}
	
	// get and show download speed
	const wxString& speed = format_size(downloaded/(_watch.Time()/1000.0));
	
	p_download_info_static->SetLabel(
			wxString::Format(_("Downloading setup.bz2 from : \n  Size : %s\n  Speed : %s/s"),
					_file_size_str.c_str(), speed.c_str())
	);
	
	p_download_gauge->SetValue(i);
}

void DownloadIniPanel::OnDownloadCompleted(wxCommandEvent &event) {
	// get error code & error string
	int err_code = event.GetInt();
	const wxString& err_str = event.GetString();
	
	if (0!=err_code) {
		wxMessageBox(err_str, wxString::Format(_("Error - code: %d"), err_code),
				wxICON_ERROR | wxCANCEL, this);
	}
	
	_watch.Pause();
	
	this->Close();
}
